package top.deepdesigner.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.deepdesigner.dto.PermissionsDTO;
import top.deepdesigner.mapper.DtsPermissionMapper;
import top.deepdesigner.pojo.DtsPermission;
import top.deepdesigner.service.DtsPermissionService;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/20 星期二 19:14:56 <br/>
 */
@Service
public class DtsPermissionServiceImpl extends ServiceImpl<BaseMapper<DtsPermission>, DtsPermission> implements DtsPermissionService {

  @Resource
  private DtsPermissionMapper permissionMapper;

  @Override
  public Set<String> getPermissionsByRoleId(Integer[] roleIds) {
    //lambda Wrapper
    LambdaQueryWrapper<DtsPermission> wrapper = new LambdaQueryWrapper<>();
    //只查没有删除的
    wrapper.eq(DtsPermission::getDeleted, 0);
    //把Integer[]转换成集合
    List<Integer> collect = new ArrayList<>(Arrays.asList(roleIds));

    wrapper.in(DtsPermission::getRoleId, collect);
    //查询
    List<DtsPermission> dtsPermissions = this.list(wrapper);
    return dtsPermissions.stream().map(DtsPermission::getPermission).collect(Collectors.toSet());
  }

  @Override
  public Set<String> getPermissionsByRoleId(Integer roleId) {
    LambdaQueryWrapper<DtsPermission> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(roleId != null, DtsPermission::getRoleId, roleId)
        .eq(true, DtsPermission::getDeleted, 0);
    return this.list(wrapper).stream().map(DtsPermission::getPermission).collect(Collectors.toSet());
  }

  @Override
  @Transactional
  public Boolean updatePermissionByRoleId(PermissionsDTO permissionsDTO) {
    List<String> permissions = permissionsDTO.getPermissions();
    Integer roleId = permissionsDTO.getRoleId();
    try {
      updateAll(roleId, permissions);
      return true;
    } catch (Exception e) {
      return false;
    }
  }

  private void updateAll(Integer roleId, List<String> newPermission) {
    //先查一次
    List<DtsPermission> list = this.list(new LambdaQueryWrapper<DtsPermission>()
        .eq(true, DtsPermission::getRoleId, roleId));

    if (list.size() == 0 || newPermission.size() == 0) {
      if (newPermission.size() != 0)
        permissionMapper.insertAll(roleId, newPermission);
      if (list.size() != 0)
        permissionMapper.updateToDeletedAll(roleId, list.stream().map(DtsPermission::getPermission).collect(Collectors.toList()));
      return;
    }
    //原始数据库数据
    Set<String> originPermission = list.stream().map(DtsPermission::getPermission).collect(Collectors.toSet());

    //权限并集
    List<String> commonPermission = originPermission.stream().filter(newPermission::contains).collect(Collectors.toList());

    //新添加的权限与之前的没有重复部分，说明全是新权限，直接添加所有的
    if (commonPermission.size() == 0) {
      permissionMapper.insertAll(roleId, newPermission);
      return;
    }
    permissionMapper.updateToNotDeletedAll(roleId, commonPermission);
    List<String> deletedPermission = originPermission.stream().filter(item -> !commonPermission.contains(item)).collect(Collectors.toList());
    if (deletedPermission.size() > 0) {
      permissionMapper.updateToDeletedAll(roleId, deletedPermission);
    }
    //insertPermission 新添加的Permission
    //新权限列表中，不在公共列表中出现的权限，被认为是新添加的权限
    List<String> insertPermission = newPermission.stream().filter(item -> !commonPermission.contains(item)).collect(Collectors.toList());
    if (insertPermission.size() > 0) {
      permissionMapper.insertAll(roleId, insertPermission);
    }
  }
}
